package com.undsf.ogd;

import com.undsf.util.CsvFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Arathi on 2015/10/19.
 */
public class Society {
    public static final int DayPerWeek = 7;
    public static final int WeekPerMonth = 4;
    public static final int WeekPerYear = 54;
    public static final int MonthPerYear = 12;

    public static Society instance;

    protected CsvFile meta;
    protected TalentMarket tMarket;
    protected List<Company> companies;
    protected List<OutSource> projects;
    protected int year;
    protected int month;
    protected int week;
    protected int day;

    public static Society getInstance(){
        if (instance == null) {
            instance = new Society("/metadata.csv");
        }
        return instance;
    }

    public static boolean WorkDistribution(Company company, int index){
        Society instance = getInstance();
        if (instance == null) return false;
        return instance.workDistribution(company, index);
    }

    public static boolean RegisterCompany(Company company){
        Society instance = getInstance();
        if (instance == null) return false;
        return instance.registerCompany(company);
    }

    public Society() {
        build("/metadata.csv");
    }

    public Society(String path){
        build(path);
    }

    public void build(String path) {
        meta = new CsvFile(path);
        tMarket = new TalentMarket(meta);
        companies = new ArrayList<>();
        projects = new ArrayList<>();
        this.year = 1;
        this.month = 4;
        this.week = 1;
        this.day = 1;
        event();
        System.out.println("构建社会完成！");
    }

    public boolean workDistribution(Company company, int id){
        //首先通过id获取到Peopleware
        Peopleware people = tMarket.getPeopleById(id);
        if (people == null){
            System.err.println("无法找到ID为"+id+"的人员！");
            return false;
        }
        System.out.println("为" + people.getName() + "分配工作单位！");
        boolean success = company.addEmployee(people);
        if (!success)
        {
            System.out.println("无法将" + people.getName() + "分配到" + company.getName());
            return false;
        }
        return true;
    }

    public boolean registerCompany(Company company) {
        System.out.println("注册公司：" + company.getName() + "...");
        companies.add(company);
        System.out.println("注册成功！");
        return false;
    }

    public static List<Peopleware> GetFreePlaners(int level) {
        Society instance = getInstance();
        if (instance == null) return null;
        return instance.getFreePlaners(level);
    }

    public List<Peopleware> getFreePlaners(int level) {
        List<Peopleware> planers = new ArrayList<>();
        for (Map.Entry<Integer, Peopleware> entry : tMarket.getAllPeople().entrySet()) {
            Peopleware people = entry.getValue();
            if (people.type == Peopleware.TypeIdFreePlaner && people.efficiency<=level)
            {
                planers.add(people);
            }
        }
        return planers;
    }

    public static List<Peopleware> GetFreeDesigners(int level) {
        Society instance = getInstance();
        if (instance == null) return null;
        return instance.getFreeDesigners(level);
    }

    public List<Peopleware> getFreeDesigners(int level) {
        List<Peopleware> planers = new ArrayList<>();
        for (Map.Entry<Integer, Peopleware> entry : tMarket.getAllPeople().entrySet()) {
            Peopleware people = entry.getValue();
            if (people.type == Peopleware.TypeIdFreeDesigner && people.efficiency<=level)
            {
                planers.add(people);
            }
        }
        return planers;
    }

    public static List<Peopleware> GetFreeSoundengs(int level) {
        Society instance = getInstance();
        if (instance == null) return null;
        return instance.getFreeSoundengs(level);
    }

    public List<Peopleware> getFreeSoundengs(int level) {
        List<Peopleware> planers = new ArrayList<>();
        for (Map.Entry<Integer, Peopleware> entry : tMarket.getAllPeople().entrySet()) {
            Peopleware people = entry.getValue();
            if (people.type == Peopleware.TypeIdFreeSoundEng && people.efficiency<=level) {
                planers.add(people);
            }
        }
        return planers;
    }

    public void elapse() {
        day++;
        if (day>DayPerWeek) {
            day=1;
            week++;
        }
        if (week>WeekPerMonth) {
            week=1;
            month++;
        }
        if (month>MonthPerYear) {
            month=1;
            year++;
        }
        //检测事件
        event();
    }

    boolean checkDate(int year, int month, int week, int day) {
        if (year==0) year=this.year;
        if (month==0) month=this.month;
        if (week==0) week=this.week;
        if (day==0) day=this.day;
        return (year==this.year && month==this.month && week==this.week && day==this.day);
    }

    public void event() {
        //每个月第一天
        if ( checkDate(0, 0, 1, 1) ) {
            System.out.println("发布新的外包项目...");
        }
        //4月1日
        if ( checkDate(0, 4, 0, 1) ) {
            if (this.year==1) {
                System.out.println("开业大吉！");
            }
            else if (this.year==2) {
                System.out.println("首年工资由国家发放。");
            }
            else {
                System.out.println("4月1日，发工资。");
            }
        }
    }
}
